<template>
	<view class="container">
		<uv-navbar title="渠道选择" bg-color="none" @leftClick="leftClick"></uv-navbar>
		<view class="channel-box">
			<view class="list-box">
				<view class="title">{{list1.title}}</view>
				<view class="list">
					<view class="list-item" v-for="(item,index) in list1.list" :key="item.channelId">
						<view class="top">
							<image :src="item.channelLogo" class="image" mode="aspectFill" />
							<uv-switch v-model="item.switchStatus == 1" size="30" activeColor="#634CF4"
								@change="onSwitch($event,item,index)" asyncChange></uv-switch>
						</view>
						<view class="name">{{item.channelName}}</view>
					</view>
				</view>
			</view>
			<view class="list-box">
				<view class="title">{{list2.title}}</view>
				<view class="list">
					<view class="list-item" v-for="(item,index) in list2.list" :key="item.channelId">
						<view class="top">
							<image :src="item.channelLogo" class="image" mode="aspectFill" />
							<uv-switch v-model="item.switchStatus == 1" size="30" activeColor="#634CF4"
								@change="onSwitch($event,item,index)" asyncChange></uv-switch>
						</view>
						<view class="name">{{item.channelName}}</view>
					</view>
				</view>
			</view>
			<view class="list-box">
				<view class="title">{{list3.title}}</view>
				<view class="list">
					<view class="list-item" v-for="(item,index) in list3.list" :key="item.channelId">
						<view class="top">
							<image :src="item.channelLogo" class="image" mode="aspectFill" />
							<uv-switch v-model="item.switchStatus == 1" size="30" activeColor="#634CF4"
								@change="onSwitch($event,item,index)" asyncChange></uv-switch>
						</view>
						<view class="name">{{item.channelName}}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="button-fb" @click="()=>{}">保存提交</view> -->
		<uv-loading-page :loading="loading" loading-mode="semicircle" bgColor="rgba(0, 0, 0, 0.2)"></uv-loading-page>
	</view>
</template>

<script>
	const app = getApp();
	import {
		getMerchantGrass,
		updateSwitchStatus
	} from '@/common/api.js'
	export default {
		data() {
			return {
				loading: false,
				list1: {
					title: '发视频/种草',
					list: []
				},
				list2: {
					title: '打卡点评',
					list: []
				},
				list3: {
					title: '商家团购',
					list: []
				}
			};
		},
		onLoad() {
			this.getData()
		},
		methods: {
			leftClick() {
				app.navigateBack()
			},
			getData() {
				getMerchantGrass({
					merchantId: this.$store.state.user.wx_merchantInfo.id,
				}).then(res => {
					res.data.forEach(item => {
						if (item.channelType == 1) {
							this.list1.title = '发视频/种草'
							this.list1.list.push(item)
						} else if (item.channelType == 2) {
							this.list2.title = '打卡点评'
							this.list2.list.push(item)
						} else if (item.channelType == 3) {
							this.list3.title = '商家团购'
							this.list3.list.push(item)
						}
					});
				})
			},
			onSwitch(e, item, index) {
				uni.showLoading();
				let list_no = 'list' + item.channelType
				let switchStatus = e ? 1 : 0
				updateSwitchStatus({
					merchantId: this.$store.state.user.wx_merchantInfo.id,
					channelId: this[list_no].list[index].channelId,
					switchStatus: switchStatus
				}).then(res => {
					this[list_no].list[index].switchStatus = e ? 1 : 0
					uni.hideLoading();
				}).catch(() => {
					uni.hideLoading();
				})
			},

			onSubmit() {
				updateSwitchStatus().then(res => {

				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		overflow: hidden;
		min-height: 100vh;
		width: 100vw;
		padding: 20rpx;
		padding-top: 216rpx;
		padding-bottom: calc(32rpx + 20rpx + 96rpx);
		box-sizing: border-box;
		background: radial-gradient(128% 128% at 95% 0%, #D7CEFF 0%, #BBE1FF 62%, #FFFFFF 95%);

		.button-fb {
			width: 480rpx;
			height: 96rpx;
			background: linear-gradient(180deg, #886AE6 0%, #4635FF 99%);
			border-radius: 48rpx;
			font-weight: 400;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 96rpx;
			text-align: center;
			margin: 0 auto;
			margin-top: 60rpx;
			position: fixed;
			bottom: 32rpx;
			left: 50%;
			transform: translateX(-50%);
		}

		.channel-box {
			width: 100%;

			.list-box {
				.title {
					font-weight: 700;
					font-size: 36rpx;
					color: #000000;
					line-height: 52rpx;
					text-align: left;
					margin-bottom: 32rpx;
				}

				.list {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-between;

					.list-item {
						width: 344rpx;
						height: 280rpx;
						background: #FFFFFF;
						border-radius: 32rpx;
						border: 2rpx solid #FFFFFF;
						box-sizing: border-box;
						padding: 32rpx;
						margin-bottom: 20rpx;

						.top {
							display: flex;
							align-items: center;
							justify-content: space-between;

							.image {
								width: 96rpx;
								height: 120rpx;
							}
						}

						.name {
							font-weight: 700;
							font-size: 32rpx;
							color: #000000;
							line-height: 36rpx;
							margin-top: 60rpx;
						}
					}
				}
			}
		}

	}
</style>